<%@ page import="POJO.*" %>
<%@ page import="DB.*" %>
<table class="centering">
    <tr><td>
            
            
            <% if (request.getParameter("page") == null || session.getAttribute("user") == null) {%>
            
            <h2>Unauthorized entrance.</h2>
            
            <% } else if (request.getParameter("username") == null) {%>
            <script type="text/javascript" src="register.js"></script>
            <div style="text-align:left;">
                <form action="index.jsp?page=register" method="post" onsubmit="return checkform(this, 'true')">
                    <table class="nice">
                        <tr><td class="b">Username:</td><td>&nbsp;&nbsp;&nbsp;<input size="30" type="text" name="username" /></td></tr>
                        <tr><td class="b">Full Name:</td><td>&nbsp;&nbsp;&nbsp;<input size="50" type="text" name="fullName"/></td></tr>
                        <tr><td class="b">Email:</td><td>&nbsp;&nbsp;&nbsp;<input size="50" type="text" name="email"/></td></tr>
                        <tr><td class="b">Make this information public:</td><td>&nbsp;&nbsp;&nbsp;<input type="checkbox" name="info" value="info"/></td></tr>
                        <tr><td class="b">Password:</td><td>&nbsp;&nbsp;&nbsp;<input size="50" type="password" name="password" /></td></tr>
                        <tr><td class="b">Repeat password:</td><td>&nbsp;&nbsp;&nbsp;<input size="50" type="password" name="password2" /></td></tr>
                        <tr><td colspan="2" align="center"><br /><br /><input type="submit" value="Register" /></td></tr>
                    </table>
                </form>
            </div>
            
            <% } else {
     if (request.getMethod().equals("POST")) {
         User u = (User) request.getSession().getAttribute("user");
         UserManager db = new UserManager(DBInfo.getConnection());

         if (!db.isConnected()) {
             out.print("<h2 class=\"nice\">No connection with the database.</h2>");
         } else if (db.usernameExists(request.getParameter("username"))) {
             out.print("<h2 class=\"nice\">A user with username " + request.getParameter("username") + " already exists.</h2>");
         } else if (request.getMethod().equals("POST") && u.Register(request.getParameter("username"), request.getParameter("password"), request.getParameter("fullName"), request.getParameter("email"), request.getParameterValues("info") != null && request.getParameterValues("info").length == 1)) {
             out.print("<h2 class=\"nice\">Registration completed.</h2>");
         } else {
             out.print("<h2 class=\"nice\">Registration failed.</h2>");
         }
     }
 }
            %>
            
    </td></tr>
</table>